﻿using AutoMapper;
using B.W.passport.Application;
using B.W.passport.Application.AdminService;
using B.W.passport.Application.DTO;
using B.W.passport.Domain;
using B.W.passport.ErrorCode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace B.W.passport.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        private readonly IAdminService adminService;
        private readonly IListService listService;
        private readonly IAddProductService addProductService;
        private readonly IDelProductService DelProductService;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="adminService"></param>
        /// <param name="listService"></param>
        /// <param name="addProductService"></param>
        /// <param name="delProductService"></param>
        public AccountController(IAdminService adminService, IListService listService, IAddProductService addProductService, IDelProductService delProductService)
        {
            this.adminService = adminService;
            this.listService = listService;
            this.addProductService = addProductService;
            this. DelProductService = delProductService;
        }





        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        [HttpGet]
        /// <returns></returns>
        public async Task<APIResult<AccountDTO>>Logion([FromQuery] string? AccountName, string? AccountPass)
        {
            var result = await adminService.Login(AccountName, AccountPass);
            return result;
        }


        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<APIPaging<Product>>> GetProduct(int PageIndex, int PageSize)
        {
            var result = await listService.GetProduct(PageIndex, PageSize);
            return result;
        }



        //[HttpPost]
        //public async Task<IActionResult> UploadImage([FromForm] int productId, [FromForm] int imageType, [FromForm] IFormFile file)
        //{
        //    if (file == null || file.Length == 0)
        //    {
        //        return BadRequest("No file uploaded.");
        //    }

        //    // 保存文件到本地
        //    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Uploads", file.FileName);
        //    using (var stream = new FileStream(filePath, FileMode.Create))
        //    {
        //        await file.CopyToAsync(stream);
        //    }

        //    // 创建 Image 实体
        //    var image = new Image
        //    {
        //        ProductId = productId,
        //        ImageType = imageType,
        //        ImageName = file.FileName,
        //        IsDeleted = false
        //    };

        //    // 保存到数据库
        //    dbContext.Images.Add(image);
        //    await dbContext.SaveChangesAsync();

        //    return Ok("Image uploaded successfully.");
        //}



        /// <summary>
        /// 上传图片到wwwroot
        /// </summary>
        /// <param name="file">图片</param>
        /// <returns>返回路径</returns>
        [HttpPost]
        public IActionResult Upload(IFormFile file)
        {
            var str = Directory.GetCurrentDirectory() + @"\wwwroot\img\" + file.FileName;
            using (var stream = new FileStream(str, FileMode.Create))
            {
                file.CopyTo(stream);
                stream.Flush();
            }
            var pic = "http://localhost:48806/img/" + file.FileName;

            //http://localhost:48806:img/
            return Ok(pic); //返回文件路径

        }


        /// <summary>
        /// 添加商品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddProduct(ProductDTO product)
        {
            return await addProductService.AddProduct(product);
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> DeleteProduct(int id)
        {
            return await DelProductService.DeleteProduct(id);
        }


        /// <summary>
        /// 修改商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdateProduct(ProductDTO product)
        {
            return await DelProductService.UpdateProduct(product);

        }

        /// <summary>
        /// 获取商品详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
         [HttpGet]
        public async Task<APIResult<Product>> GetProductById(int id)
        {
            return await listService.GetProductById(id);
        }

    }


}
